﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.UI;
using GFE.SharePoint.Interfaces;

namespace GFE.SharePoint.Lazy
{
    public class GFEMVPLazy<T> : IGFELazy<T> where T : IGFEMVPSupervisor
    {
        public delegate void GFEMVPLazyOnLoad(ref Control LoadedControl);
        public event GFEMVPLazyOnLoad OnLoad;

        Type _viewClass = default(Type);
        Type _presenterClass = default(Type);
        string _ascxPath = default(String);
        GFELazy<T> _lazy = default(GFELazy<T>);
        public GFEMVPLazy(GFELazy<T> lazy, Type ViewClass, Type PresenterClass, string AscxPath)
        {
            _lazy = lazy;
            _viewClass = ViewClass;
            _presenterClass = PresenterClass;
            _ascxPath = AscxPath;
        }

        public T Value
        {
            get { return _lazy.Value; }
        }

        public Control InvokeView(Control control)
        {
            var MVPViewControl = control.Page.LoadControl(_ascxPath);
            Object PresenterObject = Activator.CreateInstance(_presenterClass, new object[] { MVPViewControl });
            if (OnLoad != null)
                OnLoad(ref MVPViewControl);
            control.Controls.Add(MVPViewControl);
            return MVPViewControl;
        }


        public int LoadOrder
        {
            get { throw new NotImplementedException(); }
        }

       
    }
}
